Skip to content

Conversation

@max-muoto
Copy link
Contributor

This PR addressed #15169, and also makes the type more accurate by, instead of accepting str | Any for the first tuple value, simply accepting | tuple[int | str, int]. Let me know if there's context I'm missing but I'm not seeing any reason why we can't do things this way.

@max-muoto max-muoto marked this pull request as ready for review December 27, 2025 21:58
@github-actions

This comment has been minimized.

@srittau
Copy link
Collaborator

srittau commented Dec 28, 2025

You can see the problem in the primer hits: datagram_received is supposed to be overwritten in sub-classes, but those sub-classes can use a tighter annotations due to them having more knowledge on the protocol used. I think the best we can do – until we get AnyOf (python/typing#566) – is using Any.

@max-muoto
Copy link
Contributor Author

max-muoto commented Dec 28, 2025

You can see the problem in the primer hits: datagram_received is supposed to be overwritten in sub-classes, but those sub-classes can use a tighter annotations due to them having more knowledge on the protocol used. I think the best we can do – until we get AnyOf (python/typing#566) – is using Any.

I think to support this use-case the type would need to be tuple[str | Any, int, *tuple[Any, ...]] in that case. Any objections/see any better alternatives?

Edit: Needed tuple[ Any, int, *tuple[Any, ...]] for tests to pass.

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

core (https://github.com/home-assistant/core)
+ homeassistant/components/esphome/assist_satellite.py:777: error: Argument 2 of "datagram_received" is incompatible with supertype "asyncio.protocols.DatagramProtocol"; supertype defines the argument type as "tuple[Any, int, *tuple[Any, ...]]"  [override]
+ homeassistant/components/esphome/assist_satellite.py:777: note: This violates the Liskov substitution principle
+ homeassistant/components/esphome/assist_satellite.py:777: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides

anyio (https://github.com/agronholm/anyio)
+ src/anyio/_backends/_asyncio.py:1229: error: Argument 2 of "datagram_received" is incompatible with supertype "asyncio.protocols.DatagramProtocol"; supertype defines the argument type as "tuple[Any, int, *tuple[Any, ...]]"  [override]
+ src/anyio/_backends/_asyncio.py:1229: note: This violates the Liskov substitution principle
+ src/anyio/_backends/_asyncio.py:1229: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides

@max-muoto
Copy link
Contributor Author

You can see the problem in the primer hits: datagram_received is supposed to be overwritten in sub-classes, but those sub-classes can use a tighter annotations due to them having more knowledge on the protocol used. I think the best we can do – until we get AnyOf (python/typing#566) – is using Any.

I think to support this use-case the type would need to be tuple[str | Any, int, *tuple[Any, ...]] in that case. Any objections/see any better alternatives?

Edit: Needed tuple[ Any, int, *tuple[Any, ...]] for tests to pass.

I take this back, MyPy doesn't like this where-as I had luck testing with Pyright. I'll re-open assuming I get this working for both.

@max-muoto max-muoto closed this Dec 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants